from pptx.chart.data import CategoryChartData

from category.videogeneratorbase import GenerateVideoBase
from config import *


class StockIntroduceGenerateVideo(GenerateVideoBase):

    def __init__(self, res_id, ppt_tpl_path, script_data):
        GenerateVideoBase.__init__(self, res_id, ppt_tpl_path)
        self.script_data = script_data

    def fetch_script_data(self):
        print("fetch_success")

    def handle_spec_slide(self, page_no, slide, model):
        # 判断哪些页需要替换
        page_idx = page_no + 1
        if page_idx in [2, 3, 4, 14]:
            path = os.path.join(TEMPLATE_IMG_PATH, model['image_local_path'])
            self.replace_local_image(slide, slide.shapes[-1], path)
        if page_idx == 5:
            chart = slide.shapes[0].chart
            number_format = u'General'
            chart_data = CategoryChartData(number_format)
            chart_data.categories = model['categories']
            chart_data.add_series(model['series_name'], model['values'])
            chart.replace_data(chart_data)
        elif page_idx == 7:
            chart = slide.shapes[1].chart
            number_format = u'General'
            chart_data = CategoryChartData(number_format)
            chart_data.categories = model['categories']
            chart_data.add_series(model['series_name'], model['values'])
            chart.replace_data(chart_data)
        elif page_idx == 8:
            chart = slide.shapes[4].chart
            number_format = u'General'
            chart_data = CategoryChartData(number_format)
            chart_data.categories = model['categories']
            chart_data.add_series(model['series1_name'], model['values1'])
            chart_data.add_series(model['series2_name'], model['values2'])
            chart.replace_data(chart_data)
        elif page_idx == 11:
            chart = slide.shapes[1].chart
            number_format = u'General'
            chart_data = CategoryChartData(number_format)
            chart_data.categories = model['categories']
            chart_data.add_series(model['series_name'], model['values'])
            chart.replace_data(chart_data)
        elif page_idx == 12:
            shapes = slide.shapes
            holder_count = len(model['flow_top_10_holder'])
            for index, holder in enumerate(model['flow_top_10_holder']):
                row_shape = shapes[7 + index]
                row_shape.shapes[0].text_frame.paragraphs[0].runs[0].text = holder['name']
                row_shape.shapes[1].text_frame.paragraphs[0].runs[0].text = holder['hold_count']
                row_shape.shapes[2].text_frame.paragraphs[0].runs[0].text = holder['hold_change']
                row_shape.shapes[3].text_frame.paragraphs[0].runs[0].text = holder['hold_percent']
                row_shape.shapes[4].text_frame.paragraphs[0].runs[0].text = holder['hold_actual_change']
            if 10 > holder_count:
                for i in range(0, 10 - holder_count):
                    shapes[16 - i].shapes[0].text_frame.paragraphs[0].runs[0].text = ''
                    shapes[16 - i].shapes[1].text_frame.paragraphs[0].runs[0].text = ''
                    shapes[16 - i].shapes[2].text_frame.paragraphs[0].runs[0].text = ''
                    shapes[16 - i].shapes[3].text_frame.paragraphs[0].runs[0].text = ''
                    shapes[16 - i].shapes[4].text_frame.paragraphs[0].runs[0].text = ''


if __name__ == '__main__':
    res_id = '000584'
    generate = StockIntroduceGenerateVideo(res_id=res_id, ppt_tpl_path=TEMPLATE_PATH + 'stock_introduce.pptx')
    generate.fetch_script_data()
    generate.generate()
